package org.smart4j.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 编码与解码操作工具类
 *
 * @author YooLin1c
 * @version 1.0.0
 * @since 2018-01-30 20:45
 */
@Slf4j
public final class CodecUtil {
    /**
     * 将URL以URF-8编码
     *
     * @param source 原值
     * @return 编码后的字符串
     */
    public static String encodeURL(String source) {
        String target;
        try {
            target = URLEncoder.encode(source, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("encode url : ".concat(source).concat(" failed!"), e);
            throw new RuntimeException(e);
        }
        return target;
    }

    /**
     * 将URL以URF-8解码
     *
     * @param source 原值
     * @return 解码后的字符串
     */
    public static String decodeURL(String source) {
        String target;
        try {
            target = URLDecoder.decode(source, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("decode url : ".concat(source).concat(" failed!"), e);
            throw new RuntimeException(e);
        }
        return target;
    }
}
